package com.ashin;

import org.neo4j.driver.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Ashin
 */
public class TestExample {

    private final Driver driver;

    public TestExample() {
        driver = GraphDatabase.driver("neo4j://dev-neo4j.xihuanwu.com:7687", AuthTokens.basic("neo4j", "FQQbRBEXmCguisJ"));
    }

    public static void main(String[] args) {
        TestExample testExample = new TestExample();
        System.out.println(testExample.updateIntimacy());

    }


    public int commonFriendsCount() {
        try (Session session = driver.session()) {
            int count = session.readTransaction(tx -> {
                final Result result = tx.run("match (n:Account{accountId:'1455842718201565184'})-[]->(c)<-[]-(m:Account{accountId:'40333527'}) \n" +
                        "where exists((n)<-[]-(c)-[]->(m))\n" +
                        "return count(*)");
                return result.single().get(0).asInt();
            });

            return count;
        } catch (Exception e) {
            throw e;
        }
    }

    public boolean updateIntimacy() {
        try (Session session = driver.session()) {
            Map<String, Object> map = new HashMap<>();
            map.put("fromAccountId", "1476755266870333440");
            map.put("toAccountId", "1452515899113099264");
            map.put("intimacy", 200);
            boolean success = session.writeTransaction(tx -> {
                final Result result = tx.run("match (n:Account{accountId: $fromAccountId})-[f:FOLLOW]->(m:Account{accountId: $toAccountId}) \n" +
                        "set f.intimacy=$intimacy\n" +
                        "return n,f,m", map);

                return result.hasNext();
            });

            return success;

        } catch (Exception e) {
            throw e;
        }
    }
}
